# 3.21 Window Close In Dark ## 3.21.1 Overview In this project, we program to make the window automatically close when getting dark. So the photoresistor is required to sense the ambient light. We set a threshold for the resistor. When ambient light value is lower than the threshold, the servo closes the window. ## 3.21.2 Test Code ### 3.21.2.1 Code Flow ![6-21-2-1](./media/6-21-2-1-1.png) ### 3.21.2.2 Build Code There are two ways to upload the code: directly open the code file we provide; or manually build blocks. **Directly open the code file we provide:** 1. Click ![](./media/j68.png) and choose `Load from your computer` ![](./media/j67.png) 2. We have already downloaded the codes on computer desktop, so open the file and choose `3-21-automaticWindow.sb3` **Manually build blocks:** 1. Build the two basic blocks: ![6-1-4-1-1](./media/6-1-4-1-1.png) 2. We add ![](./media/j16.png) to determine the photoresistor value. If the analog value is lower than 500, the servo will rotate to 180 degree. If not, servo will rotate to 0 degree. **Complete Test Code** ![6-21](./media/6-21-2-1.png) ### 3.21.2.2 Test Result After uploading code, cover the photoresistor with something, and the servo will rotate to 180 degree. Remove the cover, and the analog value exceeds 500, so the servo rotates to 0 degree.